//
//  UITabBar+LLBadgeValue.m
//  TaoTuo
//
//  Created by admin on 2019/10/25.
//  Copyright © 2019 admin. All rights reserved.
//

#import "UITabBar+LLBadgeValue.h"

static long countMinTag = 999999;
static long dotMinTag = 888888;

@implementation UITabBar (LLBadgeValue)

- (void)showBadgeValue:(NSString *)badgeValue atIndex:(NSInteger)index
{
    UILabel *badgeLabel = [self viewWithTag:countMinTag + index];
    if (badgeLabel == nil) {
        badgeLabel = [UILabel labelTextCenterWithTitle:@"" color:[UIColor whiteColor] fontSize:9];
        badgeLabel.layer.backgroundColor = [UIColor redColor].CGColor;
        badgeLabel.tag = countMinTag + index;
        // 设置frame
        CGFloat itemW = SCREEN_WIDTH / self.items.count;
        CGFloat leftSpace = SCREEN_WIDTH * 0.5 / self.items.count + 6;
        badgeLabel.frame = CGRectMake(index * itemW + leftSpace, 5, 0, 0);
        [self addSubview:badgeLabel];
        
    }
    if (badgeValue.integerValue > 99) {
        badgeValue = @"99+";
    }
    badgeLabel.hidden = NO;
    badgeLabel.text = badgeValue;
    [badgeLabel sizeToFit];
    badgeLabel.width += 6;
    badgeLabel.height += 3;
    if (badgeLabel.width < badgeLabel.height) {
        badgeLabel.width = badgeLabel.height;
    }
    badgeLabel.layer.cornerRadius = badgeLabel.height * 0.5;
    
    if (badgeValue == nil || [badgeValue isEqualToString:@"0"]) {
        badgeLabel.hidden = YES;
    }
    
}

- (void)hiddenBadgeAtIndex:(NSInteger)index
{
    UILabel *badgeLabel = [self viewWithTag:countMinTag + index];
    badgeLabel.hidden = YES;
}


- (void)showDotAtIndex:(NSInteger)index dotColor:(UIColor *)color dotSize:(CGSize)dotSize
{
    UILabel *badgeLabel = [self viewWithTag:countMinTag + index];
    badgeLabel.hidden = YES;
    
    if (dotSize.width == 0 || dotSize.height == 0) {
        dotSize = CGSizeMake(8, 8);
    }
    
    UILabel *dotLabel = [self viewWithTag:dotMinTag + index];
    if (dotLabel == nil) {
        // 设置frame
        CGFloat itemW = SCREEN_WIDTH / self.items.count;
        CGFloat leftSpace = SCREEN_WIDTH * 0.5 / self.items.count + 6;
        
        dotLabel = [[UILabel alloc] initWithFrame:CGRectMake(index * itemW + leftSpace, 8, dotSize.width, dotSize.height)];
        dotLabel.layer.backgroundColor = color ? color.CGColor : [UIColor redColor].CGColor;
        dotLabel.layer.cornerRadius = dotSize.height * 0.5;
        
        dotLabel.tag = dotMinTag + index;
        
        [self addSubview:dotLabel];
    }
    dotLabel.hidden = NO;
}

- (void)hiddenDotAtIndex:(NSInteger)index
{
    UILabel *dotLabel = [self viewWithTag:dotMinTag + index];
    dotLabel.hidden = YES;
}

@end
